package com.qymerge.manage.system.dao.impl;

import com.qymerge.manage.core.dao.BaseDao;
import com.qymerge.manage.system.ro.SysUserRoleRO;
import com.qymerge.manage.system.dao.ISysUserRoleDao;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 *<p>Title: SysUserRoleDao</p>
 *<p>Description: </p>
 *
 *@Author cong.zhang
 *@Version 1.0.0
 *@Date 2020/7/26/0026
 *@Time 23:03
 */
@Repository
public class SysUserRoleDaoImpl extends BaseDao implements ISysUserRoleDao {

    /**
     * 查询用户所有角色
     *
     * @param userId
     * @return
     */
    @Override
    public List<SysUserRoleRO> findAllRoleByIds(Long userId) {
        String sql = "select * from dwd_MCRS_MDM_sys_user_role where user_id = ?";
        return getJdbcTemplate().query(sql, new BeanPropertyRowMapper<SysUserRoleRO>(SysUserRoleRO.class), userId);
    }

    /**
     * 根据角色查询所有
     *
     * @param roleId
     * @return
     */
    @Override
    public List<SysUserRoleRO> findAllUserByIds(Long roleId) {
        String sql = "select * from dwd_MCRS_MDM_sys_user_role where role_id = (?)";
        return getJdbcTemplate().query(sql, new BeanPropertyRowMapper<SysUserRoleRO>(SysUserRoleRO.class), roleId);
    }


    /**
     * 判断用户角色是否被使用
     *
     * @param role_id
     * @return
     */
    @Override
    public boolean ifExists(Long role_id) {
        String sql = "select count(1) from dwd_MCRS_MDM_sys_user_role where 1=1 and role_id=?";

        try {
            return getJdbcTemplate().queryForObject(sql, Integer.class, role_id) > 0;
        } catch (DataAccessException e) {
            return false;
        }
    }

    /**
     *
     * @param userId
     * @return
     */
    @Override
    public SysUserRoleRO findOneById(Long userId) {
        String sql = "select * from dwd_MCRS_MDM_sys_user_role where 1=1 and user_id = ?";
        RowMapper<SysUserRoleRO> rowMapper = new BeanPropertyRowMapper<SysUserRoleRO>(SysUserRoleRO.class);

        try {
            return getJdbcTemplate().queryForObject(sql, rowMapper, userId);
        } catch (DataAccessException e) {
            return null;
        }
    }

    /**
     * 批量增加
     *
     * @param userRoleROS
     */
    @Override
    public void insertBatch(final List<SysUserRoleRO> userRoleROS) {
        String sql="insert into dwd_MCRS_MDM_sys_user_role(user_id, role_id, create_user_id, create_time, status)" + " values(?,?,?,?,?)";

        getJdbcTemplate().batchUpdate(sql,new BatchPreparedStatementSetter() {

            @Override
            public int getBatchSize() {
                return userRoleROS.size();
            }

            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                ps.setLong(1, userRoleROS.get(i).getUser_id());
                ps.setLong(2, userRoleROS.get(i).getRole_id());
                ps.setLong(3, userRoleROS.get(i).getCreate_user_id());
                ps.setLong(4, userRoleROS.get(i).getCreate_time());
                ps.setInt(5, userRoleROS.get(i).getStatus());
            }
        });
    }

    /**
     * 删除
     *
     * @param userId
     * @return
     */
    @Override
    public int remove(Long... userId) {
        String sql = "delete from dwd_MCRS_MDM_sys_user_role where user_id in (?)";
        return getJdbcTemplate().update(sql, userId);
    }

    /**
     * 动态带条件List批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public int removeList(List<Long> ids) {
        String sql = "delete from dwd_MCRS_MDM_sys_user_role where id in (?)";
        return getJdbcTemplate().update(sql, ids);
    }


}
